#!/bin/sh
###################################################
# Shell Name: bakup.sh
# 说明:用于应用程序及配置文件备份,目前仅适用于tomcat
# Email:donnie.fan@foxmail.com
# Githab:https://github.com/hongtao425/DevOps
# Gitlab:http://gitlab.e-nci.com/DevOps/deploy.git
# 代码获取方式:git clone ssh://git@gitlab.e-nci.com:10022/DevOps/deploy.git
###################################################

set -e

#设置时期格式函数
DATE_TIME=`date +%k%M%S`
DATE_NOW=`date +%Y%m%d`
#设置程序路径
APP_PATH=`pwd | awk -F '/[^/]*$' '{print $1}'`
#工程名称
PJ_NAME=`awk -F= '/PJ_NAME/{print $2}' config/local.conf`
#工程路径
PJ_PATH=${APP_PATH}/webapps/${PJ_NAME}
#配置文件路径
CONFIG_PATH=WEB-INF/classes
#备份路径
BACKUP_PATH=${APP_PATH}/bakup

#检查备份目录
if [ -d ${APP_PATH}/bakup ]; then
    break;
else
    mkdir ${APP_PATH}/bakup
fi

if [ -d ${BACKUP_PATH}/${DATE_NOW} ];then
    break;    
else
    mkdir ${BACKUP_PATH}/${DATE_NOW}/ -p
fi

#备份程序文件
if [ -d ${BACKUP_PATH}/${DATE_NOW}/${PJ_NAME} ];then
    mv ${BACKUP_PATH}/${DATE_NOW}/${PJ_NAME} ${BACKUP_PATH}/${DATE_NOW}/${PJ_NAME}.${DATE_TIME}
fi
cp ${APP_PATH}/webapps/${PJ_NAME} ${BACKUP_PATH}/${DATE_NOW}/ -a 

#备份配置文件
cd ${BACKUP_PATH}/${DATE_NOW}/${PJ_NAME}
if [ -f log4j.properties -o -f madeinsmart.properties -o -f redis.properties ]; then
    cd ${BACKUP_PATH}/${DATE_NOW}/ && tar -zcvf ${DATE_TIME}.tgz *.properties --remove-files
fi
cp ${PJ_PATH}/${CONFIG_PATH}/*.properties ${BACKUP_PATH}/${DATE_NOW}/${PJ_NAME}


